package sudroid.net2;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import sudroid.CleanUtils;
import sudroid.json.JSONArray;
import sudroid.json.JSONException;
import sudroid.json.JSONObject;

public class Response
{
  private static ThreadLocal<DocumentBuilder> builders = new Response.1();
  private static Pattern escaped = Pattern.compile("&#([0-9]{3,5});");
  private HttpURLConnection con;
  private InputStream is;
  private Document responseAsDocument = null;
  private String responseAsString = null;
  private int statusCode;
  private boolean streamConsumed = 0;

  public Response(InputStream paramInputStream)
  {
    this.statusCode = 200;
    if ((paramInputStream instanceof BufferedInputStream));
    for (Object localObject = paramInputStream; ; localObject = new BufferedInputStream(paramInputStream))
    {
      this.is = ((InputStream)localObject);
      return;
    }
  }

  Response(String paramString)
  {
    this.responseAsString = paramString;
  }

  public Response(HttpURLConnection paramHttpURLConnection)
    throws IOException
  {
    this.con = paramHttpURLConnection;
    int i = paramHttpURLConnection.getResponseCode();
    this.statusCode = i;
    InputStream localInputStream1 = paramHttpURLConnection.getErrorStream();
    this.is = localInputStream1;
    if (localInputStream1 == null)
    {
      InputStream localInputStream2 = paramHttpURLConnection.getInputStream();
      this.is = localInputStream2;
    }
    if (this.is != null)
    {
      String str = paramHttpURLConnection.getContentEncoding();
      if ("gzip".equals(str))
      {
        InputStream localInputStream3 = this.is;
        GZIPInputStream localGZIPInputStream = new GZIPInputStream(localInputStream3);
        this.is = localGZIPInputStream;
      }
    }
    InputStream localInputStream4;
    if (this.is != null)
      localInputStream4 = this.is;
    for (Object localObject = new BufferedInputStream(localInputStream4); ; localObject = this.is)
    {
      this.is = ((InputStream)localObject);
      return;
    }
  }

  public static String unescape(String paramString)
  {
    Matcher localMatcher1 = escaped.matcher(paramString);
    StringBuffer localStringBuffer1 = new StringBuffer();
    while (true)
    {
      if (!localMatcher1.find())
      {
        StringBuffer localStringBuffer2 = localMatcher1.appendTail(localStringBuffer1);
        return localStringBuffer1.toString();
      }
      String str = Character.toString((char)Integer.parseInt(localMatcher1.group(1), 10));
      Matcher localMatcher2 = localMatcher1.appendReplacement(localStringBuffer1, str);
    }
  }

  public Document asDocument()
    throws HttpException
  {
    if (this.responseAsDocument == null);
    try
    {
      DocumentBuilder localDocumentBuilder = (DocumentBuilder)builders.get();
      byte[] arrayOfByte = asString().getBytes("UTF-8");
      ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(arrayOfByte);
      Document localDocument = localDocumentBuilder.parse(localByteArrayInputStream);
      this.responseAsDocument = localDocument;
      return this.responseAsDocument;
    }
    catch (SAXException localSAXException)
    {
      StringBuilder localStringBuilder = new StringBuilder("The response body was not well-formed:\n");
      String str1 = this.responseAsString;
      String str2 = str1;
      throw new HttpException(str2, localSAXException);
    }
    catch (IOException localIOException)
    {
    }
    throw new HttpException(localIOException);
  }

  public JSONArray asJSONArray()
    throws HttpException
  {
    String str4;
    try
    {
      String str1 = asString();
      JSONArray localJSONArray = new JSONArray(str1);
      return localJSONArray;
    }
    catch (Exception localException)
    {
      String str2 = String.valueOf(localException.getMessage());
      StringBuilder localStringBuilder = new StringBuilder(str2).append(":");
      String str3 = this.responseAsString;
      str4 = str3;
    }
    throw new HttpException(str4, localException);
  }

  public JSONObject asJSONObject()
    throws HttpException
  {
    String str4;
    try
    {
      String str1 = asString();
      JSONObject localJSONObject = new JSONObject(str1);
      return localJSONObject;
    }
    catch (JSONException localJSONException)
    {
      String str2 = String.valueOf(localJSONException.getMessage());
      StringBuilder localStringBuilder = new StringBuilder(str2).append(":");
      String str3 = this.responseAsString;
      str4 = str3;
    }
    throw new HttpException(str4, localJSONException);
  }

  public Reader asReader()
  {
    return asReader("UTF-8");
  }

  public Reader asReader(String paramString)
  {
    try
    {
      InputStream localInputStream = this.is;
      InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream, paramString);
      BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
      return localBufferedReader;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    throw new RuntimeException(localUnsupportedEncodingException);
  }

  public InputStream asStream()
  {
    if (this.streamConsumed)
      throw new IllegalStateException("Stream has already been consumed.");
    return this.is;
  }

  public String asString()
    throws HttpException
  {
    return asString("UTF-8");
  }

  public String asString(String paramString)
    throws HttpException
  {
    if (this.responseAsString == null);
    try
    {
      InputStream localInputStream = asStream();
      String str1;
      if (localInputStream == null)
      {
        str1 = null;
        return str1;
      }
      InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream, paramString);
      BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
      StringBuffer localStringBuffer1 = new StringBuffer();
      while (true)
      {
        String str2 = localBufferedReader.readLine();
        if (str2 == null)
        {
          String str3 = localStringBuffer1.toString();
          this.responseAsString = str3;
          String str4 = unescape(this.responseAsString);
          this.responseAsString = str4;
          boolean bool = CleanUtils.closeStream(localBufferedReader);
          this.con.disconnect();
          this.streamConsumed = 1;
          str1 = this.responseAsString;
          break;
        }
        StringBuffer localStringBuffer2 = localStringBuffer1.append(str2).append("\n");
      }
    }
    catch (NullPointerException localNullPointerException)
    {
      throw new HttpException(localNullPointerException);
    }
    catch (IOException localIOException)
    {
    }
    throw new HttpException(localIOException);
  }

  public void disconnect()
  {
    boolean bool = CleanUtils.closeStream(this.is);
    this.con.disconnect();
  }

  protected void finalize()
    throws Throwable
  {
    disconnect();
    super.finalize();
  }

  public String getResponseAsString()
  {
    return this.responseAsString;
  }

  public String getResponseHeader(String paramString)
  {
    if (this.con != null);
    for (String str = this.con.getHeaderField(paramString); ; str = null)
      return str;
  }

  public int getStatusCode()
  {
    return this.statusCode;
  }

  public void setResponseAsString(String paramString)
  {
    this.responseAsString = paramString;
  }

  public void setStatusCode(int paramInt)
  {
    this.statusCode = paramInt;
  }

  public String toString()
  {
    if (this.responseAsString != null);
    StringBuilder localStringBuilder5;
    HttpURLConnection localHttpURLConnection;
    for (String str1 = this.responseAsString; ; str1 = localHttpURLConnection + 125)
    {
      return str1;
      StringBuilder localStringBuilder1 = new StringBuilder("Response{statusCode=");
      int i = this.statusCode;
      StringBuilder localStringBuilder2 = localStringBuilder1.append(i).append(", response=");
      Document localDocument = this.responseAsDocument;
      StringBuilder localStringBuilder3 = localStringBuilder2.append(localDocument).append(", responseString='");
      String str2 = this.responseAsString;
      StringBuilder localStringBuilder4 = localStringBuilder3.append(str2).append(39).append(", is=");
      InputStream localInputStream = this.is;
      localStringBuilder5 = localStringBuilder4.append(localInputStream).append(", con=");
      localHttpURLConnection = this.con;
    }
  }
}

/* Location:           D:\Program Files\eclipse\apktool-r01-3\新浪微博\classes.dex.dex2jar.jar
 * Qualified Name:     sudroid.net2.Response
 * JD-Core Version:    0.6.0
 */